//
//  LHStatusViewModel.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/13.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

class LHStatusViewModel: NSObject {
    var status: LHStatus?
    
    //转发微博内容
    var retweetContent: String?
    
    //转发、评论、赞数
    var retweetCount: String?
    var commentCount: String?
    var unlikeCount: String?
    
    //微博来源
    var contentSource: String?
    
    //微博等级
    var mbrank: String?
    
    //认证类型  -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
    var verifiedType: UIImage?
    
    //发微博时间,只读属性
    var timeContent: String? {
        
        guard let creatAt = status!.created_at else {
            return nil
        }
        
        //代码执行到此，createAt一定有值

//        let dateFormatter = NSDateFormatter()
//        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
//        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
//        let createDate = dateFormatter.dateFromString(creatAt)
        
        return NSDate.createTimeWithString(creatAt).sinaTime
        
//        if isThisYear(createDate!) {
//            //是今年
//            let calendar = NSCalendar.currentCalendar()
//            if calendar.isDateInToday(createDate!) {
//                //是今天
//                let interval = abs(createDate!.timeIntervalSinceNow)
//                if interval < 60 {
//                    return "刚刚"
//                } else if interval < 3600 {
//                    let result = interval / 60
//                    return "\(Int(result))分钟前"
//                } else {
//                    let result = interval / 3600
//                    return "\(Int(result))小时前"
//                }
//            } else if calendar.isDateInYesterday(createDate!) {
//                //是昨天
//                dateFormatter.dateFormat = "昨天 HH:mm"
//            } else {
//                dateFormatter.dateFormat = "MM-dd HH:mm"
//            }
//            
//        } else {
//            //不是今年
//            dateFormatter.dateFormat = "yyyy-MM-dd"
//            
//        }
//
//        return dateFormatter.stringFromDate(createDate!)
    }
    
    init(status: LHStatus) {
        super.init()
        self.status = status
        
        handleRetWeetContent(status)
        handleSourceCount(status)
        handleMBRank((status.user?.mbrank)!)
        handleVerifiedType((status.user?.verified_type)!)
       
        retweetCount = handleContentCount(status.reposts_count, defaultTitle: "转发")
        commentCount = handleContentCount(status.comments_count, defaultTitle: "评论")
        unlikeCount = handleContentCount(status.attitudes_count, defaultTitle: "赞")
        
    }
    
    private func handleRetWeetContent(status: LHStatus) {
        
        guard let retweet = status.retweeted_status?.text else {
            return
        }
        guard let name = status.retweeted_status?.user?.screen_name else {
            return
        }
        
        //代码执行到这里转发微博和微博昵称都是存在的
        retweetContent = "@\(name)\(retweet)"
    
    }
    
    private func handleContentCount(count: Int, defaultTitle: String) -> String {
        if count > 0 {
            if count >= 10000 {
                let result = CGFloat(count/1000)/10
                print(result)
                let CountStr = "\(result)万"
                if CountStr.containsString(".0") {
                   return CountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return CountStr
            } else {
                return "\(count)"
            }
        } else {
            return defaultTitle
        }
    }
    
    private func handleSourceCount(status: LHStatus) {
        guard let source = status.source else {
            return
        }
        
        if source.containsString("\">") {
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</")?.startIndex
            if  let start = startIndex, end = endIndex {
                contentSource = source.substringWithRange(start..<end)
            }
        }
    }
    
    
    private func handleMBRank(count: Int) {
        
        if count > 0 && count < 7 {
            mbrank = "common_icon_membership_level\(count)"
        } else if count == 0 {
            mbrank = "common_icon_membership_expired"
        }
        
    }
    
    private func handleVerifiedType(count: Int) {
    
        if count == 0 {
            verifiedType = UIImage(named: "avatar_vip")
        } else if count == 2 || count == 3 || count == 5 {
            verifiedType = UIImage(named: "avatar_enterprise_vip")
        } else if count == 220 {
            verifiedType = UIImage(named: "avatar_grassroot")
        }
        
    }
    
    
//    //判断是否为今年
//    private func isThisYear(createAtDate: NSDate) -> Bool {
//        
//        let currentDate = NSDate()
//        let dateformatter = NSDateFormatter()
//        dateformatter.locale = NSLocale(localeIdentifier: "en_US")
//        
//        dateformatter.dateFormat = "yyyy"
//        
//        let currentDateYear = dateformatter.stringFromDate(currentDate)
//        let createAtDateYear = dateformatter.stringFromDate(createAtDate)
//        
//        return currentDateYear == createAtDateYear
//    }
    
    

}
